package com.frcdi.qms.web.controller.flowable;

import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.domain.R;
import com.frcdi.qms.domain.ProcessTemplates;
import com.frcdi.qms.dto.FlowEngineDTO;
import com.frcdi.qms.service.SettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;

/**
 * @author : willian fu
 * @date : 2020/9/17
 */
@RestController
@RequestMapping("admin")
@Api(tags = {"Vue2表单的CRUD接口"})
public class SettingController {

    @Autowired
    private SettingService settingService;

    /**
     * 1>
     * @param flowEngineDTO
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @PreAuthorize("@ss.hasPermi('workflow:flowable:list')")
    @ApiOperation("自定义表单的保存接口(会在此Json转Bpmn)")
    @PostMapping("/form")
    public Object saveForm(@RequestBody FlowEngineDTO flowEngineDTO) throws InvocationTargetException, IllegalAccessException {
        String s = settingService.jsonToBpmn(flowEngineDTO);
        return R.ok(s);
    }

    /**
     * 查询所有表单分组
     * @return
     */
    @PreAuthorize("@ss.hasPermi('workflow:flowable:list')")
    @GetMapping("form/group")
    public Object getFormGroups(){
        return settingService.getFormGroups();
    }

    @PreAuthorize("@ss.hasPermi('workflow:flowable:list')")
    @GetMapping("form/list")
    public AjaxResult getFormList(@RequestBody ProcessTemplates template){
        return AjaxResult.success(settingService.getFormList(template));
    }

    /**
     * 查询表单模板数据
     * @param templateId 模板id
     * @return 模板详情数据
     */
    @PreAuthorize("@ss.hasPermi('workflow:flowable:list')")
    @GetMapping("form/detail/{formId}")
    public Object getFormTemplateById(@PathVariable("formId") String templateId){
        return settingService.getFormTemplateById(templateId);
    }

    /**
     * 编辑表单
     * @param templateId 摸板ID
     * @param type 类型 stop using delete
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('workflow:flowable:list')")
    @PutMapping("form")
    public Object updateForm(@RequestParam String templateId,
                             @RequestParam String type){
        return settingService.updateForm(templateId, type);
    }

    /**
     * 编辑表单详情
     * @param template 表单模板信息
     * @return 修改结果
     */
    @PreAuthorize("@ss.hasPermi('workflow:flowable:list')")
    @PutMapping("form/detail")
    public Object updateFormDetail(@RequestBody ProcessTemplates template){
        return settingService.updateFormDetail(template);
    }
}
